package in.muton.edushare.components.alertpanel.api;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.calendar.api.CalendarInterface;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.ScheduleDAO;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.DateComparision;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.ScheduleNotifications;
import com.edushare.common.vo.Schedules;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class SchAlertMgr {

	private static final Log log = LogFactory.getLog(SchAlertMgr.class);

	public SchAlertMgr(IModel<? extends Object> obj) {
		model = obj;
	}

	public List<DateComparision> getSchedules(boolean all) {
		ScheduleDAO sDAO = new ScheduleDAO(EduShareSession.get()
				.getSessionORMInterface());
		List<Schedules> sList = new ArrayList<Schedules>();
		if (model.getObject() instanceof Universities) {
			Universities u = (Universities) model.getObject();
			sList = sDAO.getSchedulesByCreatorId(u.getUniversityId(),
					Messages.RECEIVER_TYPE_UNIVERSITY);
			log.debug("University list size =" + sList.size());
		} else if (model.getObject() instanceof Departments) {
			Departments u = (Departments) model.getObject();
			sList = sDAO.getSchedulesByCreatorId(u.getDepartmentId(),
					Messages.RECEIVER_TYPE_DEPARTMENT);
			log.debug("Department list size =" + sList.size());
		} else if (model.getObject() instanceof CourseInstances) {
			CourseInstances u = (CourseInstances) model.getObject();
			sList = sDAO.getSchedulesByCreatorId(u.getCourseInstanceId(),
					Messages.RECEIVER_TYPE_COURSE);
			log.debug("Course list size =" + sList.size());
		}
		List<DateComparision> schSorted = new ArrayList<DateComparision>();
		Collections.sort(sList, new ScheduleSorter());
		Date current = new Date();
		for(Schedules s:sList){
			if(CalendarInterface.isFutureEvent(s, current)){
				schSorted.add(s);
			}
		}
		return schSorted;
	}

	public List<DateComparision> getScheduleNotifications() {
		Users u = (Users) model.getObject();
		List<ScheduleNotifications> snL = u.getSchedules();
		List<Schedules> schList = new ArrayList<Schedules>();
		for(ScheduleNotifications sn:snL){
			if(sn.getActionStatus() == ScheduleNotifications.OPEN)
				schList.add(sn.getSchedule());
		}
		List<DateComparision> schSorted = new ArrayList<DateComparision>();
		Collections.sort(schList, new ScheduleSorter());
		Date current = new Date();
		for(Schedules s:schList){
			if(CalendarInterface.isFutureEvent(s, current)){
				schSorted.add(s);
			}
		}
		return schSorted;
	}

	IModel<? extends Object> model = null;
	private static final long serialVersionUID = 7625891327399229624L;
}
